package com.example.spring_start.controller;

import com.example.pojo.article;
import com.example.req.articleReq;
import com.example.req.articleSaveReq;
import com.example.resp.CommonResp;
import com.example.resp.PageResp;
import com.example.spring_start.service.articleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/article")
@CrossOrigin(origins = "http://localhost:8015")
public class articleController {
    @Resource
    private articleService articleService;

    @GetMapping("/getarticleList")
    public CommonResp getarticleList(articleReq articleReq){
          CommonResp<PageResp<article>> resp = new CommonResp<>();
          PageResp<article> list = articleService.getarticleList(articleReq);
          resp.setContent(list);
          return resp;
    }

    @PostMapping("/savearticle")
    public CommonResp savearticle(@RequestBody articleSaveReq req){
        CommonResp<article> resp = new CommonResp<>();
        articleService.savearticle(req);
        return resp;
    }

    @DeleteMapping("/deletearticle")
    public CommonResp deletearticle(String articleid){
        CommonResp<article> resp = new CommonResp<>();
        articleService.deletearticle(Long.parseLong(articleid));
        return resp;
    }

    @GetMapping("/getarticleByID")
    public CommonResp getarticleByID(@RequestParam String articleid) {
        CommonResp<article> resp = new CommonResp<>();
        article article = articleService.getarticleByID(Long.parseLong(articleid));
        resp.setContent(article);
        return resp;
    }
}
